import LocalValue from "../../cfw/storage/LocalValue";
import LocalMap from "../../cfw/storage/LocalMap";
import LocalList from "../../cfw/storage/LocalList";
import { ItemState } from "../../cfw/tools/Define";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName, PlayerStateID } from "../../public/Config";
import TSMap from "../../cfw/struct/TSMap";
export default class User {


    private static ins: User;

    static instance() {
        if (!this.ins) {
            this.ins = new User()
        }
        return this.ins;
    }

    protected account: string = ''
    protected coin: LocalValue;

    protected clotheState: LocalMap;
    protected typeData: LocalList;


    protected decorationTypeData: LocalList;
    protected decorationState: LocalMap;

    //使用的灯的ID
    protected lightID: LocalValue;
    //灯的状态
    protected lightState: LocalMap;

    protected treeToyState: LocalMap;
    //圣诞树玩具 
    protected treeToyTypeData: LocalList;

    protected imgData: LocalValue

    protected imageDataMap: LocalMap;

    protected dataMap: TSMap<string, LocalValue> = new TSMap()

    protected renderInfo: LocalMap

    protected renderData: LocalValue[] = []

    //八个道具的开启状态，如果不需要解锁的无视。
    protected snowToyState: LocalMap
    //使用的哪三个道具堆的雪人
    protected snowToyData: LocalList;
    //雪人身上部件的开启状态
    protected snowBodyState: LocalMap;
    //雪人部件的使用情况
    protected snowBodyTypeData: LocalList;

    //浴室用品的购买状态
    protected bathItemState: LocalMap;

    //食物的开启状态
    protected foodState: LocalMap;

    //睡觉道具开启状态
    protected sleepToyState: LocalMap;

    protected playerStateValue: LocalList;

    //宠物年龄。
    protected level: LocalValue;
    //游戏时长
    protected gameTime: LocalValue;
    //做糕点的染料瓶
    protected bottleState: LocalMap;
    //蛋糕装饰物
    protected cookieToyState: LocalMap;

    protected loginCount: LocalValue;

    protected giftState: LocalMap;

    protected maxHitCount: LocalValue;

    init(account: string) {
        this.account = account;
        this.coin = new LocalValue(account + 'coin', 0)
        this.clotheState = new LocalMap(account + 'clotheState', ItemState.NOT_GET)
        this.typeData = new LocalList(account + 'typeData', 0)
        this.decorationTypeData = new LocalList(account + 'decorationTypeData', 0)
        this.decorationState = new LocalMap(account + 'decorationState', ItemState.NOT_GET)
        this.lightID = new LocalValue(account + 'lightID', 0)
        this.treeToyState = new LocalMap(account + 'treeToyState', ItemState.NOT_GET)
        this.treeToyTypeData = new LocalList(account + 'treeToyTypeData', 0)
        this.renderInfo = new LocalMap(account + 'renderInfo', 0)
        this.imgData = new LocalValue(account + 'imgData', '')
        this.snowToyState = new LocalMap(account + 'snowToyState', ItemState.NOT_GET)
        this.snowBodyState = new LocalMap(account + 'snowBodyState', ItemState.NOT_GET)

        this.snowToyData = new LocalList(account + 'snowToyData', 0)

        this.snowBodyTypeData = new LocalList(account + 'snowBodyTypeData', 0)

        this.bathItemState = new LocalMap(account + 'bathItemState', ItemState.NOT_GET)

        this.foodState = new LocalMap(account + 'foodState', ItemState.NOT_GET)

        this.sleepToyState = new LocalMap(account + 'sleepToyState', ItemState.NOT_GET)

        this.imageDataMap = new LocalMap(account + 'imageDataMap', '')

        this.playerStateValue = new LocalList(account + 'playerStateValue', 0)

        this.level = new LocalValue(account + 'level', 1)

        this.gameTime = new LocalValue(account + 'gameTime ', 0)

        this.bottleState = new LocalMap(account + 'battleState', ItemState.NOT_GET)

        this.cookieToyState = new LocalMap(account + 'cookieToyState', ItemState.NOT_GET)
        this.lightState = new LocalMap(account + 'lightState', ItemState.NOT_GET)
        this.loginCount = new LocalValue(account + 'loginCoung', 0)
        this.loginCount.updateValue(1)

        this.giftState = new LocalMap(account + 'giftState', ItemState.NOT_GET)

        this.maxHitCount = new LocalValue(account + 'maxHitCount', 0)
    }

    setMaxHitCount(n: number) {
        this.maxHitCount.setValue(n)
    }

    getMaxHitCount() {
        return this.maxHitCount.getValue()
    }

    setGiftState(id, state) {
        this.giftState.set(id, state)
    }

    getGiftState(id) {
        return this.giftState.get(id)
    }

    getLoginCount() {
        return this.loginCount.getValue()
    }

    getTreeToyState(id) {
        return this.treeToyState.get(id)
    }

    setTreeToyState(id, state) {
        this.treeToyState.set(id, state)
    }

    getLightState(id) {
        return this.lightState.get(id)
    }

    setLightState(id, state) {
        this.lightState.set(id, state)
    }

    getDecorationState(id) {
        return this.decorationState.get(id)
    }

    setDecorationState(id, state) {
        this.decorationState.set(id, state)
    }

    getCookieToyState(id) {
        return this.cookieToyState.get(id)
    }

    setCookieToyState(id, state) {
        this.cookieToyState.set(id, state)
    }

    getBottleState(id) {
        return this.bottleState.get(id)
    }

    setBottleState(id, state) {
        this.bottleState.set(id, state)
    }

    getGameTime() {
        return this.gameTime.getValue();
    }

    setGameTime(value: number) {
        this.gameTime.setValue(value)
    }

    getLevel() {
        return this.level.getValue()
    }

    setLevel(value: number) {
        this.level.setValue(value)
        GlobalEvent.instance().emit(EventName.CHANGE_LEVEL, value)
    }

    setPlayerStateValue(typeID: PlayerStateID, value: number) {
        let index = typeID - 1;
        this.playerStateValue.set(index, value)
        GlobalEvent.instance().emit(EventName.UPDATE_PLAYER_STATE, typeID, value)
    }

    getPlayerStateValue(typeID: PlayerStateID) {
        let index = typeID - 1;
        return this.playerStateValue.get(index)
    }

    setImageData(name: string, data: string) {
        let storage = this.dataMap.get(name)
        if (!storage) {
            storage = new LocalValue(name, '')
            this.dataMap.set(name, storage)
        }
        storage.setValue(data)
        // this.imageDataMap.set(name, data)
    }

    getImageData(name: string) {
        let storage = this.dataMap.get(name)
        if (!storage) {
            storage = new LocalValue(name, '')
            if (storage.getValue() == '' && this.imageDataMap.get(name) != '') {
                storage.setValue(this.imageDataMap.get(name))
            }
            this.dataMap.set(name, storage)
        }
        return storage.getValue()
    }

    getSleepToyState(id) {
        return this.sleepToyState.get(id)
    }

    setSleepToyState(id, state) {
        this.sleepToyState.set(id, state)
    }

    getFoodItemState(id) {
        return this.foodState.get(id)
    }

    setFoodItemState(id, state) {
        this.foodState.set(id, state)
    }


    getBathItemState(id) {
        return this.bathItemState.get(id)
    }

    setBathItemState(id, state) {
        this.bathItemState.set(id, state)
    }

    getSnowBodyTypeData(index: number) {
        return this.snowBodyTypeData.get(index)
    }

    setSnowBodyTypeData(index, id) {
        cc.log(' setSnowBodyTypeData index ', index, id)
        let oldID = this.getSnowBodyTypeData(index)
        if (oldID > 0 && oldID == id) {
            id = 0;
        }
        this.snowBodyTypeData.set(index, id)
        GlobalEvent.instance().emit(EventName.SELECT_SNOW, index)
    }

    getSnowToyData(index: number) {
        return this.snowToyData.get(index)
    }

    setSnowToyData(index, id) {
        return this.snowToyData.set(index, id)
    }

    getSnowBodyState(id) {
        return this.snowBodyState.get(id)
    }

    setSnowBodyState(id, state) {
        this.snowBodyState.set(id, state)
    }

    getSnowToyState(id) {
        return this.snowToyState.get(id)
    }

    setSnowToyState(id, state) {
        this.snowToyState.set(id, state)
    }

    setImgData(data) {
        this.imgData.setValue(data)
    }

    getImgData() {
        return this.imgData.getValue();
    }


    setToyTypeData(index: number, id) {
        this.treeToyTypeData.set(index, id)
        GlobalEvent.instance().emit(EventName.SELECT_TOY, index, id)
    }

    getToyTypeData(index: number) {
        return this.treeToyTypeData.get(index)
    }

    getLightID() {
        return this.lightID.getValue()
    }

    setLightID(id: number) {
        this.lightID.setValue(id)
        GlobalEvent.instance().emit(EventName.SELECT_LIGHT, id)
    }


    getCoin() {
        return this.coin.getValue();
    }

    updateCoin(num: number) {
        this.coin.updateValue(num)
        GlobalEvent.instance().emit(EventName.UPDATE_COIN, num)
    }


    getClotheState(id: number) {
        return this.clotheState.get(id)
    }

    setClotheState(id, state: ItemState) {
        this.clotheState.set(id, state)
    }


    getTypeData() {
        return this.typeData.getValue()
    }

    setTypeData(type: number, id: number) {
        let index = type - 1;
        this.typeData.set(index, id)
        GlobalEvent.instance().emit(EventName.SELECT_CLOTHE, index, id)
    }

    setDecorationTypeData(type: number, id: number) {
        let index = type - 1;
        this.decorationTypeData.set(index, id)
        GlobalEvent.instance().emit(EventName.SELECT_DECORATION_ITEM, index, id)
    }

    getDecorationTypeData() {
        return this.decorationTypeData.getValue()
    }

}
